namespace ScrumSprintMonitor.Modules.BuildMonitor.ViewModels
{
	#region #using Directives

	using System;
	using System.Windows.Media;

	using Infrastructure;

	using Microsoft.Practices.ServiceLocation;

	#endregion

	public class FailedBuildRequesterViewModel : BuildViewModelBase,
	                                             IFailedBuildRequesterViewModel
	{
		private readonly ITeamMemberInfoProvider teamMemberInfoProvider;

		static FailedBuildRequesterViewModel()
		{
			var dependsOn = Register<FailedBuildRequesterViewModel>();

			dependsOn.Call(me => me.OnPropertyChanged("IsLastBuildBroken")).OnChanged(me => me.ReportableData.LastBuild.Status);
			dependsOn.Call(me => me.OnPropertyChanged("RequestedFor")).OnChanged(me => me.ReportableData.LastBuild.RequestedFor);
		}

		public FailedBuildRequesterViewModel(ITeamMemberInfoProvider teamMemberInfoProvider, IServiceLocator serviceLocator)
			: base(serviceLocator)
		{
			if (teamMemberInfoProvider == null)
			{
				throw new ArgumentNullException("teamMemberInfoProvider");
			}

			this.teamMemberInfoProvider = teamMemberInfoProvider;
		}

		#region IFailedBuildRequesterViewModel Members

		public bool IsLastBuildBroken
		{
			get
			{
				EnsureConnected();
				return (LastBuild != null && (LastBuild.Status == BuildStatus.Failed || LastBuild.Status == BuildStatus.PartiallySucceeded));
			}
		}

		public string RequestedFor
		{
			get
			{
				EnsureConnected();

				if (LastBuild != null && !string.IsNullOrEmpty(LastBuild.RequestedFor))
				{
					var teamMember = this.teamMemberInfoProvider.GetTeamMemberInformation(LastBuild.RequestedFor);

					if (teamMember != null)
					{
						return teamMember.DisplayAlias;
					}

					return LastBuild.RequestedFor;
				}

				return string.Empty;
			}
		}

		public Brush BackgroundBrush
		{
			get
			{
				GuardDisposed();
				return Brushes.OrangeRed;
			}
		}

		#endregion
	}
}